import time

class TopologyMapEntry:
    """
    A class which defines a single entry in the topology map every Node class holds.    
    """
    def __init__(self, originator_node, link_state_information, timestamp = None):
        """
        Constructor.
        
        originator_node - The node id which broadcasted this link state information.
        link_state_information - The neighbour list of the Node that created this entry.
        timestamp - The timestamp of this entry. The creating Node may use its own timestamp.
        """
        self._originator_node = originator_node
        self._state = link_state_information[:]
        
        if not timestamp:
            self._timestamp = time.time()
        else:
            self._timestamp = timestamp
    
    def get_originator_node(self):
        """
        Returns the node id which originated the link state information.
        """
        return self._originator_node
        
    def get_link_state(self):
        """
        Returns a copy of the link state stored in the entry.
        """
        return self._state[:]
        
    def get_timestamp(self):
        """
        Returns the timestamp of this entry.
        """
        return self._timestamp
        
    def get_entry(self):
        """
        Return a tuple containing a copy of the link state and the timestamp.
        Notice that the originator node is not returned here (as the node which created this entry
        (not the originator_node) must know to which originator_node this entry corresponds to).
        It is possible to get the originator node using get_originator_node().
        """
        return (self._state[:], self._timestamp)
        
    def clone(self):
        """
        Returns a clone of this entry.
        """
        return TopologyMapEntry(self._originator_node, self._state[:], self._timestamp)
        
    def __repr__(self):
        return "[Entry fron node %d]" % self._originator_node
